package com.doudou.base.resources.controller;

import com.doudou.base.resources.entity.dto.oauthclientdetails.AppClientDetailsAddDto;
import com.doudou.base.resources.entity.dto.oauthclientdetails.AppClientDetailsUpdateDto;
import com.doudou.base.resources.service.AppClientDetailsService;
import com.doudou.client.entity.AppClientDetailsDto;
import com.doudou.commons.entity.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 *   前端控制器
 * @author 傻男人
 * @date 2020-05-08
 */
@Slf4j
@RestController
@RequestMapping("/app")
@Api(value = "信息",tags = "信息")
public class AppClientDetailsController {

    @Autowired
    protected AppClientDetailsService appClientDetailsService;

    /**
     * 添加
     * @param appClientDetailsAddDto
     * @param result
     * @return
     */
    @ApiOperation(value = "添加",notes = "添加")
    @PostMapping(value = "/client")
    public Result addOauthClientDetails(@Valid @RequestBody @ApiParam(value = "添加dto",required = true) AppClientDetailsAddDto appClientDetailsAddDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        AppClientDetailsDto appClientDetailsDto = appClientDetailsService.addOauthClientDetails(appClientDetailsAddDto);
        return Result.ok(appClientDetailsDto);
    }

    /**
     * 根据ids删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据ids删除",notes = "根据ids删除")
    @ApiImplicitParam(name = "ids",value="ids",dataType="String",required = true,paramType = "path")
    @DeleteMapping(value = "/client/{ids}")
    public Result deleteOauthClientDetails(@PathVariable("ids") String ids) {
        Integer number = appClientDetailsService.deleteOauthClientDetails(ids);
        return Result.ok(number);
    }

    /**
     * 根据id修改
     * @param id
     * @param appClientDetailsUpdateDto
     * @param result
     * @return
     */
    @ApiOperation(value = "根据id修改",notes = "修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value="id",dataType="Long",required = true,paramType = "path")})
    @PutMapping(value = "/client/{id}")
    public Result updateOauthClientDetails(@PathVariable("id")  Long id, @Valid @RequestBody @ApiParam(value = "修改dto",required = true) AppClientDetailsUpdateDto appClientDetailsUpdateDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        appClientDetailsUpdateDto.setId(id);
        Integer sum = appClientDetailsService.updateOauthClientDetails(appClientDetailsUpdateDto);
        return Result.ok(sum);
    }

    /**
     * 根据ids查询
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据ids查询",notes = "根据ids查询")
    @ApiImplicitParam(name = "ids",value="ids",dataType="String",required = true,paramType = "path")
    @GetMapping(value = "/client/{ids}")
    public Result findOauthClientDetails(@PathVariable("ids")  String ids) {
        List<AppClientDetailsDto> list = appClientDetailsService.findOauthClientDetails(ids);
        return Result.ok(list);
    }

    /**
    * 根据id查询
    * @param primaryId   的id
    * @return
    */
    @ApiOperation(value = "根据id查询",notes = "根据id查询")
    @ApiImplicitParam(name = "primaryId",value="的id",dataType="Long",required = true,paramType = "path")
    @GetMapping(value = "/client/{primaryId}/primaryId")
    public Result findOauthClientDetails(@PathVariable("primaryId")Long primaryId) {
        AppClientDetailsDto result = appClientDetailsService.findOauthClientDetailsById(primaryId);
        return Result.ok(result);
    }
}
